package LeetCode;

public class LC_110_BalancedBinaryTree {
    public static void main(String[] args) {

    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Solution {
        private int minDepth = 0;
        private int maxDepth = 0;

        boolean isBalanced(TreeNode root) {
            if (root == null) return true;
            int left = depth(root.left);
            int right = depth(root.right);
            return left - right <= 1 && left - right >= -1 && isBalanced(root.left) && isBalanced(root.right);
        }

        private int depth(TreeNode n) {
            if (n == null) return 0;
            if (n.left == null && n.right == null) return 1;
            else {
                int left = depth(n.left);
                int right = depth(n.right);
                return 1 + (left > right ? left : right);
            }
        }

    }

}
